// pages/talent_detail/talent_detail.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
	recomList: [],
	resumeFiles:[],
    showActionTip: false,
    contact_phone:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({resumeId: options.id});
    this.getResumeDetail();
    //显示前5个相似简历
    // core.get({url:'resume/similar_resumes',data:{id:this.data.resumeId},success:(ret)=>{
    //   this.setData({recomList:ret.data.resumes.slice(0,5)});
    // }});
    //客服电话
    // core.get({url:'help/getphone',data:{},success:(ret)=>{
    //   this.setData({contact_phone:ret.data.phone});
	// }});
	let page = this;
	core.get({url:'/xilujob.resume/resume_files_prev',data:{resume_id:options.id},success(ret, response) {
        page.setData({resumeFiles: ret.data});
      }})
    app.eventBus.on(app.Events.UserLogin, this.eventOnUserLogin, this);
  },
  getResumeDetail() {
    core.get({
      url: '/xilujob.resume/resume_detail', data: {id:this.data.resumeId},
      success: ret => {
        let resume = ret.data.resume;
        this.setData({
          resume: resume, 
          isFav: ret.data.is_fav, 
          isInterviewing: ret.data.is_interviewing,
          hasActionLog: ret.data.has_action_log, 
          actionNum: ret.data.action_num
        })
      },fail: ret => {
       // wx.navigateBack();
      }
    });
  },
  bindMakeCall(event) {
    let resume = this.data.resume;
    if(!resume.personal.mobile){
      wx.showToast({title: '对方未填写电话号码',icon:'none'});
      return;
    }
    if(resume.personal.mobile.indexOf('*')!==-1) {
      core.post({url:'xilujob.resume/get_contact',data:{resume_id:resume.id,user_id:resume.user_id},success:ret=>{
        this.setData({'resume.personal.mobile':ret.data.mobile,'resume.personal.email':ret.data.email});
        wx.makePhoneCall({
          phoneNumber: ret.data.mobile //仅为示例，并非真实的电话号码
        });
      },fail:ret=>{
        wx.showModal({
          title: "提示",
          content: ret.msg,
          success(res){
            if(res.confirm){
              wx.navigateTo({
                url: '/pages/taocan/taocan',
              })
            }
          }
        })
        return false;
      }});
      return;
    }
    wx.makePhoneCall({phoneNumber: event.currentTarget.dataset.phone});
  },
  //沟通
  contact(){
    if(!app.getUserinfo(true)) {
      return;
	}
	let userinfo = app.getUserinfo(true);
    if (this.data.resume.user_id == userinfo.id) {
      wx.showToast({title: '不可与自己沟通',icon: 'none'})
      return false;
    }
    let chatParams = {
	  receive_user_id: this.data.resume.user_id,
	  resume_id: this.data.resume.id,
      content: this.data.resume.id,
	  to_username: this.data.resume.user ? this.data.resume.user.nickname : '',
	  chat_type: 5, //人才沟通
    }
    wx.navigateTo({
      url: '/pages/communicate/index',
      success(res) {
        res.eventChannel.emit('chatTransfor', chatParams)
      }
    })
  },
  // 收藏
  bindToggleFav() {
    if(!app.getUserinfo(true)) {
      return;
    }
    if(app.getUserinfo().group_id==1) {
      wx.showToast({title:'请切换到公司身份',icon:'none'});
      return;
    }
    let page = this;
    page.setData({isFav: !this.data.isFav});
    core.post({url: '/xilujob.resume/toggle_fav',loading:false,data:{id: this.data.resumeId},success: (ret) => {
      page.setData({isFav: ret.data.is_fav});
      if(this.data.isFav) {
        app.eventBus.emit(app.Events.ResumeFavAdd, {resume: this.data.resume});
      }else {
        app.eventBus.emit(app.Events.ResumeFavRemove, {resume: this.data.resume});
      }
    },fail:ret=>{
      wx.showToast({title: '收藏失败',icon:'none'});
      page.setData({isFav: !page.data.isFav});
      return false;
    }});
  },
  eventOnUserLogin(userinfo) {
    this.getResumeDetail();
  },
  onShareAppMessage() {
    let title = '西陆 - 简历(' + this.data.resume.personal.realname + ')';
    return {
      title: title,
      path: '/pages/talent_resume_detail/talent_resume_detail?id='+this.data.resumeId,
    };
  },
  bindCreateInterview() {
    if(!app.getUserinfo(true)) {
      return;
    }
    if(app.getUserinfo().group_id!=2) {
      wx.showToast({title:'请切换到公司身份',icon:'none'});
      return;
    }
    if(this.data.isInterviewing) {
      return;
    }
    // if(!this.data.hasActionLog && this.data.actionNum==0) {
    //   this.setData({actionTipMsg: ret.msg});
    //   this.actionTip();
    //   return;
    // }
    let resume = this.data.resume;
    core.post({url:'xilujob.resume/get_interview',data:{resume_id:resume.id,user_id:resume.user_id},success:ret=>{
      wx.navigateTo({
        url: '/pages/interview_take/interview_take',
        events: {
          dataTransfer: data => {
            this.setData({isInterviewing:true});
          }
        },
        success: res => {
          res.eventChannel.emit('dataTransfer', {resume:this.data.resume});
        }
      });
    },fail:ret=>{
      wx.showModal({
        title: "提示",
        content: ret.msg,
        success(res){
          if(res.confirm){
            wx.navigateTo({
              url: '/pages/taocan/taocan',
            })
          }
        }
      })
      return false;
    }});

  },
  bindDownload() {
    let resume = this.data.resume;
    core.post({url:'xilujob.resume/get_contact',data:{resume_id:resume.id,user_id:resume.user_id},success:ret=>{
      this.setData({'resume.personal.mobile':ret.data.mobile,'resume.personal.email':ret.data.email});
      core.post({
        url: 'xilujob.resume/download',data:{id:this.data.resumeId},
        success: ret => {
          let url = ret.data.jpg_path;
          wx.previewImage({
            urls: [url], current: url,
          });
        }
      });
    },fail:ret=>{
      if(ret.code==-1) {
        this.setData({actionTipMsg: ret.msg});
        this.actionTip();
        return false;
      }
    }});
  },
  actionTip() {
    this.setData({showActionTip: !this.data.showActionTip});
  },
   actionTipTwo(e) {
    wx.makePhoneCall({phoneNumber: e.currentTarget.dataset.phone});
    this.setData({showActionTip: !this.data.showActionTip});
  },
  share() {
    this.shareTip();
    core.get({url:'xilujob.resume/poster_info/id/'+this.data.resumeId, success:ret=>{
      core.setCache('temp',ret.data,10);
      wx.navigateTo({
        url: '/pages/share_post_new/share_post_new'
      })
    }});
  },
  shareTip() {
    this.setData({showShareTip: !this.data.showShareTip});
  },
  bindViewImages(event) {
    let index = event.currentTarget.dataset.index;
    wx.previewImage({
      urls: [this.data.resumeFiles[index].file],
      current: this.data.resumeFiles[index].file
    })
  },

  bindViewFile(event){
	let index = event.currentTarget.dataset.index;
	let url = this.data.resumeFiles[index].file;
	wx.downloadFile({
		// 示例 url，并非真实存在
		url: url,
		success: function (res) {
		  const filePath = res.tempFilePath
		  wx.openDocument({
			filePath: filePath,
			success: function (res) {
			  console.log('打开文档成功')
			}
		  })
		}
	  })
  }
});
